/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import controller.AtividadeDAO;
import javax.swing.DefaultListModel;
import models.Atividade;
import models.Projeto;
import utils.enums.atividade.StatusAtividade;
import utils.nucleo.Nucleo;

/**
 *
 * @author OliverKra
 */
public class CadAtividade extends javax.swing.JPanel {

    private Atividade at;
    private Projeto p;

    public CadAtividade(Projeto p){
        this(p, false);
    }
    
    public CadAtividade(Projeto p, Atividade at){
        this(p, true);
        this.at = at;
        carregaStatus();
        carregaCampos();
    }
    
    private CadAtividade(Projeto p, boolean edicao) {
        initComponents();
        
        if (!edicao){
            this.at = new Atividade();
            carregaStatus();
        }
        
        this.p = p;
    }
    
    private void carregaCampos(){
        this.jTFNome.setText(this.at.getNome());
        this.jTFCategoria.setText(this.at.getCategoria());
    }
    
    protected void carregaStatus(){
        DefaultListModel<StatusAtividade> lista = new DefaultListModel<>();
        int selectedIndex = 0;

        for( StatusAtividade sa : StatusAtividade.values() ) {
            lista.addElement(sa);
            
            if ( sa.equals(at.getStatus()) ) {
                selectedIndex = lista.size()-1;
            }
        }

        jLSStatus.setModel(lista);
        jLSStatus.setSelectedIndex(selectedIndex);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTFNome = new javax.swing.JTextField();
        jTFCategoria = new javax.swing.JTextField();
        jTBSalvar = new javax.swing.JToggleButton();
        jTBCancelar = new javax.swing.JToggleButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jLSStatus = new javax.swing.JList();

        setPreferredSize(new java.awt.Dimension(429, 441));

        jLabel1.setText("Cadastro de atividades!");

        jLabel2.setText("Nome:");

        jLabel3.setText("Categoria:");

        jLabel4.setText("Situação:");

        jTBSalvar.setText("Salvar");
        jTBSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBSalvarActionPerformed(evt);
            }
        });

        jTBCancelar.setText("Cancelar");
        jTBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBCancelarActionPerformed(evt);
            }
        });

        jLSStatus.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jLSStatus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTFCategoria, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                            .addComponent(jTFNome)
                            .addComponent(jScrollPane2))))
                .addContainerGap(76, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTBSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTBCancelar)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTFNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTFCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTBSalvar)
                    .addComponent(jTBCancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBCancelarActionPerformed
        Nucleo.getInstance().getPrincipal().fecharTela(this);
    }//GEN-LAST:event_jTBCancelarActionPerformed

    private void jTBSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBSalvarActionPerformed
        at.setNome(jTFNome.getText());
        at.setCategoria(jTFCategoria.getText());
        at.setStatus((StatusAtividade) jLSStatus.getSelectedValue());
        at.setCodigoProjeto(this.p.getId());
        AtividadeDAO.inserir(at);
        Nucleo.getInstance().getPrincipal().fecharTela(this);
    }//GEN-LAST:event_jTBSalvarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jLSStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToggleButton jTBCancelar;
    private javax.swing.JToggleButton jTBSalvar;
    private javax.swing.JTextField jTFCategoria;
    private javax.swing.JTextField jTFNome;
    // End of variables declaration//GEN-END:variables
}
